package com.gexiaobao.pigeon.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gexiaobao.pigeon.R;
import com.gexiaobao.pigeon.app.model.bean.GoodsSkuResponse;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by babosa on 2017/4/11.
 */

public class SingleLevelAdapter extends BaseAdapter {
    private final Context context;
    private final List<GoodsSkuResponse.ListBean> list;
    private final LayoutInflater inflater;
    public static Map<Integer, Boolean> isSelected;

    public SingleLevelAdapter(Context context, List<GoodsSkuResponse.ListBean> list) {
        this.list = list;
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        isSelected = new HashMap<>();
        for (int i = 0; i < list.size(); i++) {
            isSelected.put(i, false);
        }
    }

    //清楚   把所有状态清楚为未选中状态  多级单选
    public void clear() {
        for (int i = 0; i < list.size(); i++) {
            isSelected.put(i, false);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint({"SetTextI18n", "UseCompatLoadingForDrawables"})
    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (view == null) {
            view = inflater.inflate(R.layout.child_layout, viewGroup, false);
            viewHolder = new ViewHolder();
            viewHolder.title = (TextView) view.findViewById(R.id.child_text);
            viewHolder.price = (TextView) view.findViewById(R.id.child_text_price);
            viewHolder.checkBox = (CheckBox) view.findViewById(R.id.my_CheckBox);
            viewHolder.check_ll = (LinearLayout) view.findViewById(R.id.item_check_ll);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.title.setText(list.get(position).getSpecName());
        viewHolder.price.setText("¥ " + list.get(position).getSalePrice());
        if (Boolean.TRUE.equals(isSelected.get(position))) {
            viewHolder.check_ll.setBackground(context.getResources().getDrawable(R.drawable.round_sku_car_check25_b_g));
        } else {
            viewHolder.check_ll.setBackground(context.getResources().getDrawable(R.drawable.round_sku_car_gray25_b_g));
        }
        return view;
    }

    public static class ViewHolder {
        public TextView title;
        public TextView price;
        public CheckBox checkBox;
        public LinearLayout check_ll;
    }
}
